﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Web.UI.WebControls;
using Foxy.GridViewControls;

namespace Foxy.WebControls
{
    public partial class GridView : System.Web.UI.WebControls.GridView
    {
        private List<int> listSelected;

        #region Properties

        [Category("ExtendedProperties"), Description("Identify whether a checkbox column must be generated"), DefaultValue(typeof(bool),"false")]
        public bool GenerateCheckBoxColumn { get; set; }

        int checkBoxColumnIndex = 0;
        [Category("ExtendedProperties"), Description("Indicates the 0-based position of the checkbox column"), DefaultValue(typeof(int),"")]
        public int CheckBoxColumnIndex
        {
            get { return checkBoxColumnIndex; }
            set { checkBoxColumnIndex = (value < 0 ? 0 : value); }
        }

        [Category("ExtendedProperties"), Description("The DataField that we wish our control to bind to."), DefaultValue(typeof(string), "")]
        public string CheckBoxDataField { get; set; }

        [Category("ExtendedProperties"), Description("The string element using into header text box."), DefaultValue(typeof(string), "<input type='checkbox' hidefocus='true' id='{0}' name='{0}' {1} onclick='CheckAll(this)'>")]
        public string CheckBoxHeader { get; set; }

        [Category("ExtendedProperties"), Description("Identify whether a checkbox element generate the postback event"), DefaultValue(typeof(bool), "false")]
        public bool EnableAutoPostbackCheckBox { get; set; }

        [Category("ExtendedProperties"), Description("Identify whether rollover event is activated"), DefaultValue(typeof(bool), "false")]
        public bool ActivateRollOverColor { get; set; }

        [Category("ExtendedProperties"), Description("Set the roll over color"), DefaultValue(typeof(Color), "")]
        public Color RowRollOverColor
        {
            get
            {
                if (ViewState["RowRollOverColor"] != null)
                    return (Color)ViewState["RowRollOverColor"];
                return Color.Transparent;
            }
            set { ViewState["RowRollOverColor"] = value; }
        }

        [Category("ExtendedProperties"), Description("Identify whether double click row event is activated"), DefaultValue(typeof(bool), "false")]
        public bool EnableRowDoubleClickEvent { get; set; }

        [Category("ExtendedProperties"), Description("Set the roll over text"), DefaultValue(typeof(Color), "")]
        public Color RowRollOverText
        {
            get
            {
                if (ViewState["RowRollOverText"] != null)
                    return (Color)ViewState["RowRollOverText"];
                return Color.Transparent;
            }
            set { ViewState["RowRollOverText"] = value; }
        }

        [Category("ExtendedProperties"), Description("Identify whether click row event is activated"), DefaultValue(typeof(bool), "false")]
        public bool EnableRowClickEvent { get; set; }


        internal virtual List<int> SelectedIndices
        {
            get
            {
                listSelected = new List<int>();
                for (int i = 0; i < Rows.Count; i++)
                {
                    CheckBox cb = Rows[i].FindControl(CheckBoxItem.CheckBoxID) as CheckBox;
                    if (cb == null)
                        return listSelected;
                    if (cb.Checked)
                        listSelected.Add(i);
                }
                return listSelected;
            }
        }

        public virtual int[] GetSelectedIndices()
        {
            return SelectedIndices.ToArray();
        }

        #endregion
    }
}
